package com.loufeng.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import com.loufeng.dao.AreaDAO;
import com.loufeng.dao.SiteDAO;
import com.loufeng.po.AreaPO;
import com.loufeng.po.SitePO;
import com.yunform.framework.hibernate.QueryClause;
import com.yunform.framework.util.serialize.JsonUtil;

@Controller
@Path("index")
public class IndexRest {
	@Autowired
	SiteDAO siteDAO;
	@Autowired
	AreaDAO areaDAO;
	
	@GET
	@Path("")
	public ModelAndView index(){
		ModelAndView modelAndView = new ModelAndView("index.ftl");
		List<AreaPO> areaPOList = areaDAO.selectByParam(new QueryClause().andEqual("parId", 1).addOrder(Order.asc("seq")));
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		for(AreaPO areaPO : areaPOList){
			List<SitePO> sitePOList = siteDAO.selectByParam(new QueryClause().andEqual("subAreaId", areaPO.getId()).addOrder(Order.desc("monthVisitQuantity")));
			if(sitePOList.size()==0) continue;
			Map<String,Object> map = JsonUtil.convertValue(areaPO, HashMap.class);
			map.put("sitePOList", sitePOList);
			list.add(map);
		}
		modelAndView.addObject("list",list);
		return modelAndView;
	}
}
